import Vue from 'vue'
import App from './App.vue'
import router from './router'
import axios from "axios";
import qs from "qs";
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
// 设置原型 this.$http.post
Vue.prototype.$http = axios;
// 设置默认的路径为后台服务器的ip和端口
Vue.prototype.$http.defaults.baseURL = "http://localhost:8333";
// 设置qs原型
Vue.prototype.$qs = qs;
Vue.use(ElementUI);
Vue.config.productionTip = false

//设置axios的请求拦截器
axios.interceptors.request.use(config => {
  //拦截到请求后，将token加到请求头中
  let accessToken = localStorage.getItem("access-token");
  let refreshToken = localStorage.getItem("refresh-token");
  console.log("拦截请求，读取到token：" + accessToken);
  if(accessToken && refreshToken){
    config.headers.Authorization = accessToken;
    config.headers.RefreshToken = refreshToken;
  }
  return config;
})
//使用axios拦截器获得刷新的token
axios.interceptors.response.use(config => {
  //token过期就保存刷新后的token
  if(config.headers.tokens){
    let tokens = JSON.parse(config.headers.tokens);
    console.log(tokens.accessToken)
    console.log(tokens.refreshToken)
    if(tokens.accessToken && tokens.refreshToken){
      localStorage.setItem("access-token",tokens.accessToken)
      localStorage.setItem("refresh-token",tokens.refreshToken)
    }
  }
  return config;
})

new Vue({
  router,
  render: function (h) { return h(App) }
}).$mount('#app')
